﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Data;
using System.Windows.Controls;
using System.ComponentModel.Composition;
using ShredTheLog.Api;
using System.Diagnostics.Contracts;

namespace ShredTheLog.ViewModels
{
    [ValueConversion(typeof(object), typeof(IEnumerable<Control>))]
    public sealed class UserMenuItemsConverter : IValueConverter
    {
        [ImportMany]
        private Lazy<ICommandListItem, IMenuCommandMetadata>[] _userMenuCommands = null;
        private IEnumerable<Lazy<ICommandListItem, IMenuCommandMetadata>> UserMenuCommands 
        { 
            get 
            {
                Contract.Assume(_userMenuCommands != null);

                return _userMenuCommands.Where(i => i.Metadata.Menu == Menus.User); 
            } 
        }

        public UserMenuItemsConverter()
        {
            Globals.CompositionContainer.SatisfyImportsOnce(this);
        }

        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            return MenuItemsConverter.Default.Convert(UserMenuCommands);
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotSupportedException();
        }

        public static readonly UserMenuItemsConverter Default = new UserMenuItemsConverter();
    }
}
